# encoding: utf-8

class Workpackage < ActiveRecord::Base
  belongs_to  :project,
              :touch    =>  true

  has_many  :tasks,
            :dependent  =>  :destroy

  accepts_nested_attributes_for :tasks, 
                                :reject_if => lambda { |a| a[:name].blank? },
                                :allow_destroy => true

	# validations
	validates :project_id,
            :presence     =>  true,
            :numericality =>  { :only_integer => true,
                                :greater_than => 0  }

  validates :name,
            :presence => true,
            :length   => {  :within => 5..100, :unless => "self.name.empty?" }

  validates :status,
            :presence  => true,
            :inclusion => { :in => ['initial', 'in_progress', 'delayed', 'finished']  }

  validates :progress,
            :presence     =>  true,
            :numericality =>  { :only_integer             =>  true,
                                :greater_than_or_equal_to =>  0,
                              	:less_than_or_equal_to    =>  100 }

	validates :description,
            :length =>  { :within => 5..500 }

end

# == Schema Information
#
# Table name: workpackages
#
#  id          :integer         not null, primary key
#  project_id  :integer
#  name        :string(255)
#  description :text
#  status      :string(255)     default("initial")
#  progress    :integer         default(0)
#  milestone   :boolean         default(FALSE)
#  created_at  :datetime
#  updated_at  :datetime
#

